package com.hns.can.mainte.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.hns.can.common.aop.orgTree.LeftOrgAnnotation;
import com.hns.can.mainte.entity.CommCarChargeInfoEntity;
import com.hns.can.master.entity.BaseEqmtInfoEntity;
import com.hns.can.master.upgrade.UpgradeProcessor;
import com.hns.tool.pub.PubUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.task.TaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hns.can.base.entity.SysUserEntity;
import com.hns.can.mainte.entity.UpgEqmtVrsnManageInfo;
import com.hns.can.mainte.entity.UpgEqmtVrsnManageLog;
import com.hns.can.mainte.vo.UpgradeVo;
import com.hns.frame.service.CommonService;
import com.hns.tool.pub.UtilString;

/**
 * 应用升级管理业务类
 * @author:zhangzq
 * @email:
 * @version Revision 2.0.0
 */
@Service
public class CommonUpgradeService extends CommonService {
	
    @Autowired
    private TaskExecutor taskExecutor;
	
	/**
	 * 新增日志
	 * @param logEntity
	 * @author:zhangzq
	 * @email:
	 */
    @Transactional
    public void saveUpgLog(UpgEqmtVrsnManageLog logEntity) {        
        commonEntityDao.save(logEntity);
    }
	
	/**
	 * 根据Id获取设备版本管理信息实体类
	 * @param VrsnId
	 * @return
	 * @author:zhangzq
	 * @email:
	 */
    @Transactional
    public UpgEqmtVrsnManageInfo getUpgEqmtVrsnByVrsnId(String VrsnId){
        UpgEqmtVrsnManageInfo upgEqmtVrsnManageInfo = commonEntityDao.find(VrsnId, UpgEqmtVrsnManageInfo.class);
        return upgEqmtVrsnManageInfo;
    }

    @Transactional
    @LeftOrgAnnotation()
    public void setAirTemplateForCar(UpgradeVo vo, SysUserEntity user) throws Exception{
        SimpleDateFormat  sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<BaseEqmtInfoEntity> list = PubUtil.convertListMapToObject(sqlMapDao.selectList("airConditioner.getEqmtInfoList", vo), BaseEqmtInfoEntity.class);
        UpgEqmtVrsnManageInfo upgEqmtVrsnManageInfo = getUpgEqmtVrsnByVrsnId(vo.getVrsnId());
        if(PubUtil.isNotEmpty(list)){
            for(int i=0;i<list.size();i++){
                BaseEqmtInfoEntity eqmt = list.get(i);
                //新增日志
                UpgEqmtVrsnManageLog logEntity = new UpgEqmtVrsnManageLog();
                logEntity.setLogId(UtilString.getUUID());
                logEntity.setLogType("ULOG0009");
                logEntity.setCorpId(vo.getCorpId());
                logEntity.setEqmtId(eqmt.getEqmtId());
                logEntity.setCarId(eqmt.getRltvCarId());
                logEntity.setVrsnId(upgEqmtVrsnManageInfo.getVrsnId());
                logEntity.setUpgradeBeginTime(sd.format(new Date()));
                logEntity.setUpgradeSt("UPST0200");
                logEntity.setStMsg("UPSM0101");
                logEntity.setUpgradeUser(user.getAcctName());
                saveUpgLog(logEntity);

                //开启后台升级进程
                String upgradeCommandStr="";
                if(upgEqmtVrsnManageInfo.getAppUpgrade().equals("AIRC0001")){
                    upgradeCommandStr = "{'devId': '"+ eqmt.getEqmtCd() +"','canId': '17', 'ac':'"+upgEqmtVrsnManageInfo.getAcSt()+"','operateStatus':'"+ upgEqmtVrsnManageInfo.getAirCondiStmOprtSt() +"','fanStatus': '"+ upgEqmtVrsnManageInfo.getAirCondiStmFanOprtSt() +"','minTemp': '"+ upgEqmtVrsnManageInfo.getAirCondiLowestTept() +"','maxTemp': '"+ upgEqmtVrsnManageInfo.getAirCondiHighestTept() +"'}";
                }
                else if(upgEqmtVrsnManageInfo.getAppUpgrade().equals("AIRC0002")){
                    upgradeCommandStr = "{'devId': '"+ eqmt.getEqmtCd() +"','canId': '17','paramId': '241', 'evaporateGears':'"+upgEqmtVrsnManageInfo.getAirCondiStmFanOprtSt()+"','setTemp':'"+ upgEqmtVrsnManageInfo.getAirCondiLowestTept() +"','ac': '"+ upgEqmtVrsnManageInfo.getAcSt() +"','operateStatus': '"+ upgEqmtVrsnManageInfo.getAirCondiStmOprtSt() +"','freshOpenTime': '"+ upgEqmtVrsnManageInfo.getFreshAirOpenTime()+"','freshCloseTime': '"+ upgEqmtVrsnManageInfo.getFreshAirCloseTime()+"','lockStatus': '"+ upgEqmtVrsnManageInfo.getClockCtrl() +"'}";
                }
                else if(upgEqmtVrsnManageInfo.getAppUpgrade().equals("AIRC0003")){
                    upgradeCommandStr = "{'devId': '"+ eqmt.getEqmtCd() +"','canId': '17','paramId': '241', 'evaporateGears':'"+upgEqmtVrsnManageInfo.getAirCondiStmFanOprtSt()+"','teptLimitDw': '"+ upgEqmtVrsnManageInfo.getAirCondiLowestTept() +"','teptLimitUp': '"+ upgEqmtVrsnManageInfo.getAirCondiHighestTept() +"','ac': '"+ upgEqmtVrsnManageInfo.getAcSt() +"','operateStatus': '"+ upgEqmtVrsnManageInfo.getAirCondiStmOprtSt() +"','freshOpenTime': '"+ upgEqmtVrsnManageInfo.getFreshAirOpenTime()+"','freshCloseTime': '"+ upgEqmtVrsnManageInfo.getFreshAirCloseTime()+"','lockStatus': '"+ upgEqmtVrsnManageInfo.getClockCtrl() +"'}";
                }
                else if(upgEqmtVrsnManageInfo.getAppUpgrade().equals("AIRC0004")){
                    upgradeCommandStr = "{'devId': '"+ eqmt.getEqmtCd() +"','canId': '17','paramId': '242', 'evaporateGears':'"+upgEqmtVrsnManageInfo.getAirCondiStmFanOprtSt()+"','setTemp':'"+ upgEqmtVrsnManageInfo.getAirCondiLowestTept() +"','ac': '"+ upgEqmtVrsnManageInfo.getAcSt() +"','operateStatus': '"+ upgEqmtVrsnManageInfo.getAirCondiStmOprtSt() +"','disinfectSt': '"+ upgEqmtVrsnManageInfo.getDisinfectSt()+"','freshAirSt': '"+ upgEqmtVrsnManageInfo.getFreshAirSt() +"'}";
                }
                UpgradeProcessor processor = new UpgradeProcessor("17", upgradeCommandStr, taskExecutor);
                processor.process();
            }
        }

    }

	/**
	 * 空调模版升级
	 * @param vo
	 * @param user
	 * @throws Exception
	 * @author:zhangzq
	 * @email:
	 */
    @Transactional
    public void saveAirpgLog(UpgradeVo vo, SysUserEntity user) throws Exception{
        for (int i = 0; i < vo.getEqmtIdArr().length; i++) {
            SimpleDateFormat  sd = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            UpgEqmtVrsnManageInfo upgEqmtVrsnManageInfo = getUpgEqmtVrsnByVrsnId(vo.getVrsnIdArr()[i]);
            //新增日志
            UpgEqmtVrsnManageLog logEntity = new UpgEqmtVrsnManageLog();
            logEntity.setLogId(UtilString.getUUID());
            logEntity.setLogType("ULOG0009");
            logEntity.setCorpId(vo.getCorpId());
            logEntity.setEqmtId(vo.getEqmtIdArr()[i]);
            logEntity.setCarId(vo.getCarIdArr()[i]);
            logEntity.setVrsnId(upgEqmtVrsnManageInfo.getVrsnId());
            logEntity.setUpgradeBeginTime(sd.format(new Date()));
            logEntity.setUpgradeSt("UPST0200");
            logEntity.setStMsg("UPSM0101");
            logEntity.setUpgradeUser(user.getAcctName());
            saveUpgLog(logEntity);
            
            //开启后台升级进程
            String upgradeCommandStr="";
            if(upgEqmtVrsnManageInfo.getVrsnType().equals("VERS0007")){
            	upgradeCommandStr = "{'devId': '"+ vo.getEqmtCdArr()[i] +"','canId': '17', 'ac':'"+upgEqmtVrsnManageInfo.getAcSt()+"','operateStatus':'"+ upgEqmtVrsnManageInfo.getAirCondiStmOprtSt() +"','fanStatus': '"+ upgEqmtVrsnManageInfo.getAirCondiStmFanOprtSt() +"','minTemp': '"+ upgEqmtVrsnManageInfo.getAirCondiLowestTept() +"','maxTemp': '"+ upgEqmtVrsnManageInfo.getAirCondiHighestTept() +"'}";
            }
            else if(upgEqmtVrsnManageInfo.getVrsnType().equals("VERS0008")){
            	upgradeCommandStr = "{'devId': '"+ vo.getEqmtCdArr()[i] +"','canId': '17','paramId': '241', 'evaporateGears':'"+upgEqmtVrsnManageInfo.getAirCondiStmFanOprtSt()+"','setTemp':'"+ upgEqmtVrsnManageInfo.getAirCondiLowestTept() +"','ac': '"+ upgEqmtVrsnManageInfo.getAcSt() +"','operateStatus': '"+ upgEqmtVrsnManageInfo.getAirCondiStmOprtSt() +"','freshOpenTime': '"+ upgEqmtVrsnManageInfo.getFreshAirOpenTime()+"','freshCloseTime': '"+ upgEqmtVrsnManageInfo.getFreshAirCloseTime()+"','lockStatus': '"+ upgEqmtVrsnManageInfo.getClockCtrl() +"'}";
            }
            else if(upgEqmtVrsnManageInfo.getVrsnType().equals("VERS0009")){
            	upgradeCommandStr = "{'devId': '"+ vo.getEqmtCdArr()[i] +"','canId': '17','paramId': '241', 'evaporateGears':'"+upgEqmtVrsnManageInfo.getAirCondiStmFanOprtSt()+"','teptLimitDw': '"+ upgEqmtVrsnManageInfo.getAirCondiLowestTept() +"','teptLimitUp': '"+ upgEqmtVrsnManageInfo.getAirCondiHighestTept() +"','ac': '"+ upgEqmtVrsnManageInfo.getAcSt() +"','operateStatus': '"+ upgEqmtVrsnManageInfo.getAirCondiStmOprtSt() +"','freshOpenTime': '"+ upgEqmtVrsnManageInfo.getFreshAirOpenTime()+"','freshCloseTime': '"+ upgEqmtVrsnManageInfo.getFreshAirCloseTime()+"','lockStatus': '"+ upgEqmtVrsnManageInfo.getClockCtrl() +"'}";
            }
            UpgradeProcessor processor = new UpgradeProcessor("17", upgradeCommandStr, taskExecutor);
            processor.process();
            
        }
    }
}
